import express from 'express';
import cors from 'cors';

const app = express();
const port = process.env.PORT ? Number(process.env.PORT) : 8787;
const apiKey = process.env.DEEPSEEK_API_KEY || '';

app.use(cors());
app.use(express.json({ limit: '1mb' }));

app.post('/api/chat', async (req, res) => {
  try {
    if (!apiKey) {
      res.status(500).json({ error: 'Missing DEEPSEEK_API_KEY' });
      return;
    }
    const { messages } = req.body || {};
    const r = await fetch('https://api.deepseek.com/v1/chat/completions', {
      method: 'POST',
      headers: {
        'Authorization': `Bearer ${apiKey}`,
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        model: 'deepseek-chat',
        messages: messages ?? [{ role: 'user', content: '你好' }],
        temperature: 0.7
      })
    });
    const data = await r.json();
    res.json(data);
  } catch (e: any) {
    res.status(500).json({ error: e?.message || 'Unknown error' });
  }
});

app.listen(port, () => {
  console.log(`[server] listening on http://localhost:${port}`);
});


